/**
 EventMobi
 Copyright (C) 2011  Igor Costa igorcosta AT riacycle DOT com
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.fms
{
	import flash.net.NetConnection;
	import flash.net.SharedObject;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.IOErrorEvent
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	
	[Event(name="success", type="flash.events.Event")]
	[Event(name="failed", type="flash.events.Event")]
	
	public class FMSConnection extends NetConnection implements IEventDispatcher
	{
		/**
		 *  Constructor
		 */
		public function FMSConnection() 
		{
			super();
			
		}
		
		public var clientID:Number;
		
		/**
		 *  Connect
		 */
		override public function connect( url:String, ...args ):void
		{
			this.objectEncoding = flash.net.ObjectEncoding.AMF0;
			NetConnection.defaultObjectEncoding
			
			this.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
			this.addEventListener( SecurityErrorEvent.SECURITY_ERROR, netSecurityError );
			
			super.connect( url );
		}
		
		
		/**
		 *  setID
		 */
		public function setId( id:Number ):*
		{
			if( isNaN( id ) ) return;
			clientID = id;
			return "Okay";
		}
		
		/**
		 * 	Status Handler for the NetConnection class
		 */
		private function netStatusHandler( event:NetStatusEvent ):void
		{
			switch( event.info.code ) {
				case "NetConnection.Connect.Success":
					
					dispatchEvent( new Event( "success" ) );
					break;
				case "NetConnection.Connect.Failed":
					dispatchEvent( new Event( "failed" ) );
					break;			
				default:
					break;
			}
		}
		
		private function netSecurityError( event:SecurityErrorEvent ):void {
			
		}	
		
	}
}